# -*- coding: utf-8 -*-
##############################################################################
#
#
##############################################################################

from odoo.report import report_sxw
from odoo import models,api


class sale_delivery_py(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(sale_delivery_py, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'get_lot_ids': self.get_lot_ids,
            'get_base': self.get_base,
            'get_t': self.get_t,
            'get_custom_product_code':self.get_custom_product_code,
            'get_qty_assist_sum':self.get_qty_assist_sum,
            'get_product_name':self.get_product_name,
            'group_pack_operations': self.group_pack_operations,
        })
        self.context = context

    def get_lot_ids(self,product_id_parm):
        result = []
        for lot in product_id_parm.lot_ids:
            result.append(lot.name)
        return result

    def get_base(self,picking_id_para):
        self.cr.execute("""\
                    select saleOrderName,concat(pt.name,'(',pp.default_code,')')as product_name,sp.client_order_ref,product_customer_code,spol.lot_uom_qty,pu.name lotuom_name,spol.qty,outeruom.uom,concat(spl.ref,'-',spl.name) from stock_picking sp
                    left join stock_pack_operation spo on spo.picking_id=sp.id
                    left join stock_pack_operation_lot spol on spol.operation_id=spo.id
                    left join product_uom pu on pu.id=spol.lot_uom
                    left join (select spo.product_id,pu.name uom from stock_picking sp
                        left join stock_pack_operation spo on spo.picking_id=sp.id
                        left join product_uom pu on pu.id=spo.product_uom_id where sp.name=\'%s\') outeruom on outeruom.product_id=spo.product_id
                    left join (select distinct(sol.product_id),so.name saleOrderName,sol.product_customer_code from stock_picking sp
                        left join stock_move sm on sm.picking_id=sp.id
                        left join procurement_order po on po.id=sm.procurement_id
                        left join sale_order_line sol on po.sale_line_id=sol.id
                        left join sale_order so on sol.order_id=so.id
                        where sp.name=\'%s\') outermove on outermove.product_id=spo.product_id
                    left join stock_production_lot spl on spl.id = spol.lot_id
                    left join product_product pp on spo.product_id = pp.id
                    left join product_template pt on pp.product_tmpl_id=pt.id
                    where sp.name=\'%s\'"""%(picking_id_para,picking_id_para,picking_id_para))
        return self.cr.fetchall()

    def get_t(self,para):
        self.cr.execute("""\
        select sm.product_id,sm.product_uom_qty from stock_picking sp
        left join stock_move sm on sp.id = sm.picking_id
        left join (select *from stock_picking where name=\'%s\') oute on oute.id=sp.id where sp.name=\'%s\'
        """%(para,para))
        return self.cr.fetchall()

    def get_custom_product_code(self,pick_para,product_para):
        self.cr.execute("""\
            select distinct(sol.product_id),sol.product_customer_code from stock_picking sp
                left join stock_move sm on sm.picking_id=sp.id
                left join procurement_order po on po.id=sm.procurement_id
                left join sale_order_line sol on po.sale_line_id=sol.id
                left join sale_order so on sol.order_id=so.id
                where sp.name=\'%s\' and sol.product_id=%s
        """%(pick_para,product_para))
        return self.cr.fetchall()

    def get_qty_assist_sum(self, para):
        self.cr.execute("""\
        select sum(spol.lot_uom_qty) from stock_picking sp
        left join stock_pack_operation spo on spo.picking_id=sp.id
        left join stock_pack_operation_lot spol on spol.operation_id=spo.id where sp.name=\'%s\'
        """ % para)
        return self.cr.fetchall()

    @api.multi
    def get_product_name(self, para):
        prodt=self.objects.env['product.product'].search([('id','=',para)])
        name = prodt.name+'('
        for index,tt in enumerate(prodt.attribute_value_ids):
            if index<len(prodt.attribute_value_ids)-1:
                name=name+tt.name+','
            else:
                name=name+tt.name+')'
        print prodt.attribute_value_ids
        return name

    def group_pack_operations(self, pack_operation_product_ids):
        products = pack_operation_product_ids.mapped("product_id")
        return {
            product: pack_operation_product_ids.filtered(lambda o:o.product_id == product) for product in products
        }


class report_sale_deliveryCar(models.AbstractModel):
    _name = 'report.sale_delivery_print.report_sale_delivery_car'
    _inherit = 'report.abstract_report'
    _template = 'sale_delivery_print.report_sale_delivery_car'
    _wrapped_report_class = sale_delivery_py


class ReportSaleDeliveryDiamond(models.AbstractModel):
    _name = 'report.sale_delivery_print.report_sale_delivery_diamond'
    _inherit = 'report.abstract_report'
    _template = 'sale_delivery_print.report_sale_delivery_diamond'
    _wrapped_report_class = sale_delivery_py


class StockPickingReport(models.AbstractModel):
    _name = 'report.sale_delivery_print.stock_picking_report'
    _inherit = 'report.abstract_report'
    _template = 'sale_delivery_print.stock_picking_report'
    _wrapped_report_class = sale_delivery_py


class StockPickingReportPurchase(models.AbstractModel):
    _name = 'report.sale_delivery_print.stock_picking_report_purchase'
    _inherit = 'report.abstract_report'
    _template = 'sale_delivery_print.stock_picking_report_purchase'
    _wrapped_report_class = sale_delivery_py

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
